/* Copyright 2009 Palm, Inc.  All rights reserved. */

var SystemService = Class.create({ 
	initialize: function() { 
	}
});


SystemService.systemserviceIdentifier = 'palm://com.palm.systemservice';
SystemService.preferencesIdentifier = 'palm://com.palm.preferences/appProperties';
SystemService.wanIdentifier = 'palm://com.palm.wan';
SystemService.applicationmanagerIdentifier = 'palm://com.palm.applicationManager/open';
SystemService.telephonyIdentifier = 'palm://com.palm.telephony';
SystemService.provisioningIdentifier = 'palm://com.palm.provisioning';
SystemService.carrierDBIdentifier = 'palm://com.palm.data.carriernetworksettings';
SystemService.messagingIdentifier = 'palm://com.palm.messaging';

SystemService.setSystemPreferencesService = function(key, value, callback) {
	 params = {}
     params[key] = value;
	var request = new Mojo.Service.Request(SystemService.systemserviceIdentifier, {
			method: 'setPreferences',
			parameters: params,
			onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
			onFailure: function(response) {
					if(callback) 
						callback(response)
					},
		});
	return request;
}

SystemService.getSystemPreferencesService = function(values, callback) {
	if (!values) {
		callback();
		return;
	}
	
	if (!Object.isArray(values)) {
		values = [values];
	}
	
	var request = new Mojo.Service.Request(SystemService.systemserviceIdentifier, {
			method: 'getPreferences',
			parameters: {'keys': values}, 
			onSuccess: callback,
			onFailure: callback
		});
	return request;
}

SystemService.getShortCutNumbers = function(callback) {
	var keys = ['4DigitNumber', '5DigitNumber', '6DigitNumber', '7DigitNumber'];
	var request = new Mojo.Service.Request(SystemService.systemserviceIdentifier, {
			method: 'getPreferences',
			parameters: {'keys': keys}, 
			onSuccess: callback,
			onFailure: callback
		});
	return request;
}

SystemService.getRoamguardService = function(callback) {
    var appId = "com.palm.wan";
	var request = new Mojo.Service.Request(SystemService.preferencesIdentifier, {
            method: 'Get',
            parameters: {
                'appId': appId,
                'key': 'roamguard'
            },
			onSuccess: callback,
			onFailure: callback
		});
	return request;
	
}

SystemService.setRoamguardService = function(status, callback){
    var request = new Mojo.Service.Request(SystemService.wanIdentifier, {
        method: 'set',
        parameters: {
            'roamguard': (status == "true") ? 'disable' : 'enable'
        },
        onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
        onFailure: function(response) {
					if(callback) 
						callback(response)
					}
    });
	return request;
}

SystemService.setWANService = function(status, callback){
    var request = new Mojo.Service.Request(SystemService.wanIdentifier, {
        method: 'set',
        parameters: {
            'disablewan': status
        },
        onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
        onFailure: function(response) {
					if(callback) 
						callback(response)
					}
    });
	return request;
}

SystemService.autoConnectWANService = function(status, callback){
    var request = new Mojo.Service.Request(SystemService.wanIdentifier, {
        method: 'set',
        parameters: {
            'autoconnect': (status === true) ? 'enable' : 'disable'
        },
        onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
        onFailure: function(response) {
					if(callback) 
						callback(response)
					}
    });
	return request;
}

SystemService.getWANStatus = function(callback){
    var request = new Mojo.Service.Request(SystemService.wanIdentifier, {
        method: 'getstatus',
        parameters: {
            'subscribe': true
        },
        onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
        onFailure: function(response) {
					if(callback) 
						callback(response)
					}
    });
	return request;
}

SystemService.getManualDataSettings = function(callback) {
	var request = new Mojo.Service.Request(SystemService.carrierDBIdentifier, {
        method: 'getCarrierNetworkSettingsRecord',
        parameters: {
            'searchOverride': "force"
        },
        onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
        onFailure: function(response) {
					if(callback) 
						callback(response)
					}
    });
	return request;
}

SystemService.setManualDataSettings = function(settings, callback) {
	var request = new Mojo.Service.Request(SystemService.carrierDBIdentifier, {
        method: 'setOverideRecordSettings',
        parameters: settings,
        onSuccess: function(response) {
					if(callback) 
						callback(true, response)
					},
        onFailure: function(response) {
					if(callback) 
						callback(false, response)
					}
    });
	return request;
}

SystemService.enableManualDataSettings = function(use, callback) {
	var request = new Mojo.Service.Request(SystemService.carrierDBIdentifier, {
        method: 'setOverideRecordUse',
        parameters: {
			'useOverride': use
		},
        onSuccess: function(response) {
					if(callback) 
						callback(true, response)
					},
        onFailure: function(response) {
					if(callback) 
						callback(false, response)
					}
    });
	return request;
}

SystemService.testManualDataSettings = function(settings, callback) {
	
	var request = new Mojo.Service.Request(SystemService.wanIdentifier, {
        method: 'manageprofile',
        parameters: {
            "action":"connect",
			"wanprofile": {
				"cid": "1",
				"service":"test",
				"username":settings.username,
				"password":settings.password,
				"pdptype":"IP",
				"apn":settings.apn,
				"pdpaddr":"0.0.0.0",
				"dcomp":0,
				"hcomp":"0"
			}
			
        },
        onSuccess: function(response) {
				SystemService.testDataSettingsSubscribe(callback, settings);
		},					
        onFailure: function(response) {
					if(callback) 
						callback(false, response)
					}
    });
	return request;
	
}

SystemService.testDataSettingsSubscribe = function(callback, settings) {
	SystemService.dataSettingsReq = new Mojo.Service.Request(SystemService.wanIdentifier, {
        method: 'getstatus',
        parameters: {
            'subscribe': true
        },
        onSuccess: function(response){
			if (response && response.connectedservices) {
				response.connectedservices.each(function(service) {
					if (service.service.indexOf("test") !== -1 ) {
						var result = undefined;
						if (service.connectstatus === "active") {
							result = true;
							
						} else if (service.connectstatus === "disconnected" || service.requeststatus === "connect failed") {
							result = false;
						} 
						if (result !== undefined) {
							if (settings && settings.disconnect === true) {
								SystemService.testDataSettingsDisconnect();
							}
							callback(result);
							SystemService.dataSettingsReq.cancel();
						}
					}
				})			
			}
		},	
		onFailure: function(response){
			if (callback) 
				callback(false, response)
			}
    });
	
},

SystemService.testDataSettingsDisconnect = function() {
	var request = new Mojo.Service.Request(SystemService.wanIdentifier, {
        method: 'manageprofile',
        parameters: {
            "action":"disconnect",
			"wanprofile": {
				"cid": "1",
			}
			
        },
        onSuccess: function(response) {
				Mojo.Log.info("WAN test: successfully disconnected");
		},					
        onFailure: function(response){
			Mojo.Log.info("WAN test: unsuccessfully disconnected");
		}
    });
	return request;
}

SystemService.testMmsSettings = function(settings, callback) {
	
	var request = new Mojo.Service.Request(SystemService.messagingIdentifier, {
        method: 'mms/validateMmscSettings',
        parameters: {
			"mmsc": settings.mmsc,
			"mmsProxy": settings.mmsProxy,
			"apn":"test"
		},
        onSuccess: function(response) {
					if(callback)
						if (response.mmscSettingsValid === true) {
							callback(true, response);
						} else {
							callback(false, response);
						} 
					},
        onFailure: function(response) {
					if(callback) 
						callback(false, response)
					}
    });
	return request;
}

SystemService.relaunchPhoneprefsService = function(value, callback){
    var request = new Mojo.Service.Request(SystemService.applicationmanagerIdentifier, {
        parameters: {
            'id': "com.palm.app.phoneprefs",
            params: {
                'launchType': value,
            }
        },
		onSuccess: callback,
		onFailure: callback
    });
	return request;
}

SystemService.relaunchPhoneService = function(value, callback){
    var request = new Mojo.Service.Request(SystemService.applicationmanagerIdentifier, {
        parameters: {
            id: 'com.palm.app.phone',
            params: {
                "number": number,
				"locale": Mojo.Locale.current
            }
        },
		onSuccess: callback,
		onFailure: callback
    });
	return request;
}


SystemService.getCarrierName = function(callback) {
	
	var request = new Mojo.Service.Request(SystemService.carrierDBIdentifier, {
		method: 'getCarrierName',
		parameters: {},
		onSuccess: callback,
		onFailure: callback,
    });	
	return request;
}
